/****** Object:  Stored Procedure [dbo].spChiTietPhieuNhapInsert Script Date: 6/12/2012 ******/
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'[dbo].[spChiTietPhieuNhapInsert]') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
	DROP PROCEDURE [dbo].[spChiTietPhieuNhapInsert]
GO

/******************************************************************************
**		Change History
*******************************************************************************
**		Date:		Author:					Description:
**		--------	--------				-----------------------------------
**		23/06/2012	SongTM					Innit created
*******************************************************************************/

CREATE PROCEDURE [dbo].[spChiTietPhieuNhapInsert]
(
	@MaCTPN nvarchar(12) output,
	@SoPN nvarchar(14),
	@MaLuuKho int,
	@DonGia int,
	@SoLuong int
)
WITH ENCRYPTION

AS

SET NOCOUNT ON
if not exists (select * from [tbl_ChiTietPhieuNhap])
	Set @MaCTPN=1
else
	Set @MaCTPN=(select RIGHT(MAX([MaCTPN]),9) from [tbl_ChiTietPhieuNhap])+1
Set @MaCTPN='CTN'+REPLICATE('0',9-LEN(@MaCTPN))+@MaCTPN  
INSERT INTO [tbl_ChiTietPhieuNhap]
(
	[MaCTPN],
	[SoPN],
	[MaLuuKho],
	[DonGia],
	[SoLuong]
)
VALUES
(
	@MaCTPN,
	@SoPN,
	@MaLuuKho,
	@DonGia,
	@SoLuong
)
GO

--GRANT EXECUTE ON [dbo].[spChiTietPhieuNhapInsert] TO [sa]
--GO
